﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UvsChess;

namespace StudentAI
{
    public class MoveValidation
    {        
        /// <summary>
        /// Pawn Moves
        /// Forward 1 if not blocked
        /// Forward 2 if both spots not blocked, must be in starting position
        /// Diagonal 1 if opposite color is in that position
        /// En Passante
        /// Pawn Promotion - Pawn becomes Knight, Bishop, Rook, Queen if the Pawn reaches opposite side (always pick queen)
        /// </summary>
        /// <param name="prevBoard"></param>
        /// <param name="move"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        static public bool validatePawnMove(ChessBoard prevBoard, ChessMove move, ChessColor color, ChessLocation kingPosition) {
            int forward = 0, initialPosition;
            if (color == ChessColor.Black)
            {
                initialPosition = 1;
                forward = 1;
            }
            else if (color == ChessColor.White)
            {
                initialPosition = 6;
                forward = -1;
            }
            else return false;
            int yDiff = move.To.Y - move.From.Y;
            int xDiff = move.To.X - move.From.X;
            //Make sure it's on the board
            if(MoveIsOffBoard(move)) return false;
            
            //Make sure To and From are not same color
            ChessPiece toPiece = prevBoard[move.To];
            if (SameColor(toPiece, color)) return false;

            //Make sure it's forward 1 or 2 spaces
            if (yDiff != forward && yDiff != forward + forward) return false;
            //Check one space forward
            if (yDiff == forward && xDiff == 0 && prevBoard[move.To] == ChessPiece.Empty) return true;
            //If starting, check two forward
            if (move.From.Y == initialPosition && yDiff == forward * 2 && xDiff == 0 && prevBoard[move.To] == ChessPiece.Empty) return true;
            //Check forward, x+
            if (yDiff == forward && xDiff == 1 && MoveGeneration.isEnemyPiece(prevBoard[move.To], color)) return true;
            //Check forward, x-
            if (yDiff == forward && xDiff == -1 && MoveGeneration.isEnemyPiece(prevBoard[move.To], color)) return true;

            return false;
        }//End validatePawnMove

        static public bool validateRookMove(ChessBoard prevBoard, ChessMove move, ChessColor color, ChessLocation kingPosition)
        {
            int yDiff = move.To.Y - move.From.Y;
            int xDiff = move.To.X - move.From.X;
            //Make sure it's on the board
            if (MoveIsOffBoard(move)) return false;
            //Check for no move
            if (xDiff == 0 && yDiff == 0) return false;
            //Check that one is not zero, and the other is zero
            if (xDiff != 0 && yDiff != 0) return false;
            //Check if the move To is the same color
            if (SameColor(prevBoard[move.To.X,move.To.Y], color)) return false;
            //Check if any space in between is not empty
            if (xDiff > 0) {
                for (int i = move.From.X + 1; i < move.To.X; i++) {
                    if (prevBoard[i, move.To.Y] != ChessPiece.Empty) return false;
                }
            }
            else if (xDiff < 0) {
                for (int i = move.To.X + 1; i < move.From.X; i++)
                {
                    if (prevBoard[i, move.To.Y] != ChessPiece.Empty) return false;
                }
            }
            else if (yDiff > 0) {
                for (int i = move.From.Y + 1; i < move.To.Y; i++) {
                    if (prevBoard[move.To.X,i] != ChessPiece.Empty) return false;
                }
            }
            else if (yDiff < 0) {
                for (int i = move.To.Y + 1; i < move.From.Y; i++) {
                    if (prevBoard[move.To.X, i] != ChessPiece.Empty) return false;
                }
            }
            ChessBoard newBoard = new ChessBoard(prevBoard.ToPartialFenBoard());
            newBoard.MakeMove(move);
            ChessLocation newKingPosition;
            if (move.From == kingPosition)
                newKingPosition = move.To;
            else newKingPosition = kingPosition;
            if (kingInCheck(newBoard, newKingPosition, color))
                return false;
            return true;
        }//End validateRookMove

        static public bool validateBishopMove(ChessBoard prevBoard, ChessMove move, ChessColor color, ChessLocation kingPosition)
        {
            int yDiff = move.To.Y - move.From.Y;
            int xDiff = move.To.X - move.From.X;
            //Make sure it's on the board
            if (MoveIsOffBoard(move)) return false;
            //Check if the move To is the same color
            if (SameColor(prevBoard[move.To.X, move.To.Y], color)) return false;
            //Check for no move
            if (xDiff == 0 && yDiff == 0) return false;
            //Make sure it's a valid position
            if (Math.Abs(xDiff) != Math.Abs(yDiff)) return false;
            //Check if any space in between is not empty
            if (xDiff < 0 && yDiff < 0) 
            {
                int x = move.To.X + 1;
                int y = move.To.Y + 1;
                for (int i = 0; i < Math.Abs(xDiff)-1; i++)
                {                    
                    if (prevBoard[x++,y++] != ChessPiece.Empty) return false;
                }
            }
            else if (xDiff < 0 && yDiff > 0)
            {
                int x = move.To.X + 1;
                int y = move.To.Y - 1;
                for (int i = 0; i < Math.Abs(xDiff)-1; i++)
                {
                    if (prevBoard[x++,y--] != ChessPiece.Empty) return false;
                }
            }
            else if (xDiff > 0 && yDiff < 0)
            {
                int x = move.From.X + 1;
                int y = move.From.Y - 1;
                for (int i = 0; i < Math.Abs(xDiff)-1; i++)
                {
                    if (prevBoard[x++,y--] != ChessPiece.Empty) return false;
                }
            }
            else if (xDiff > 0 && yDiff > 0)
            {
                int x = move.From.X + 1;
                int y = move.From.Y + 1;
                for (int i = 0; i < Math.Abs(xDiff)-1; i++)
                {
                    if (prevBoard[x++,y++] != ChessPiece.Empty) return false;
                }
            }
            ChessBoard newBoard = new ChessBoard(prevBoard.ToPartialFenBoard());
            newBoard.MakeMove(move);
            ChessLocation newKingPosition;
            if (move.From == kingPosition)
                newKingPosition = move.To;
            else newKingPosition = kingPosition;
            if (kingInCheck(newBoard, newKingPosition, color))
                return false;
            return true;
        }//End validateBishopMove

        static public bool validateKnightMove(ChessBoard prevBoard, ChessMove move, ChessColor color, ChessLocation kingPosition)
        {
            int yDiff = move.To.Y - move.From.Y;
            int xDiff = move.To.X - move.From.X;
            //Make sure it's on the board
            if (MoveIsOffBoard(move)) return false;
            //Check if the move To is the same color
            if (SameColor(prevBoard[move.To.X,move.To.Y], color)) return false;
            //Check that 2 moves and 1 move
            if (Math.Abs(yDiff) != 1 && Math.Abs(yDiff) != 2 ||
                Math.Abs(xDiff) != 1 && Math.Abs(xDiff) != 2) return false;
            //Verify valid position
            if (Math.Abs(yDiff) == 2 && Math.Abs(xDiff) == 1 ||
                Math.Abs(xDiff) == 2 && Math.Abs(yDiff) == 1)
            {
                ChessBoard newBoard = new ChessBoard(prevBoard.ToPartialFenBoard());
                newBoard.MakeMove(move);
                ChessLocation newKingPosition;
                if (move.From == kingPosition)
                    newKingPosition = move.To;
                else newKingPosition = kingPosition;
                if (kingInCheck(newBoard, newKingPosition, color))
                    return false;
                return true;
            }
            return false;
            
        }//End validateKnightMove

        static public bool validateKingMove(ChessBoard prevBoard, ChessMove move, ChessColor color, ChessLocation kingPosition)
        {
            //Check in all 8 directions
            int fromX = move.From.X, fromY = move.From.Y;
            int yDiff = move.To.Y - move.From.Y;
            int xDiff = move.To.X - move.From.X;
            //Check that it's on the board
            if (MoveIsOffBoard(move)) return false;
            //Check from position
            if (xDiff == 0 && yDiff == 0) return false;
            //Check that To isn't same color
            if (SameColor(prevBoard[move.To.X, move.To.Y], color)) return false;
            //Check that it's within range [-1,0,1]
            if (xDiff < -1 || xDiff > 1 || yDiff < -1 || yDiff > 1) return false;

            ChessBoard newBoard = new ChessBoard(prevBoard.ToPartialFenBoard());
            newBoard.MakeMove(move);
            ChessLocation newKingPosition;
            if (move.From == kingPosition)
                newKingPosition = move.To;
            else newKingPosition = kingPosition;
            if (kingInCheck(newBoard, newKingPosition, color))
                return false;
            return true;
        }//End validateKingMove

        static public bool validateQueenMove(ChessBoard prevBoard, ChessMove move, ChessColor color, ChessLocation kingPosition) {
            //check diagnols using biship validation
            //check horizontal an vertical using rook validation
            if (validateBishopMove(prevBoard, move, color, kingPosition) || validateRookMove(prevBoard, move, color, kingPosition))
                return true;
            else
                return false;
        }//End validateQueenMove
        
        static public bool knightCheckingKing(ChessBoard board, ChessLocation kingLocation, ChessLocation testLocation, ChessColor color)
        {
            ChessLocation CL = new ChessLocation(kingLocation.X, kingLocation.Y);
            ChessMove testX = new ChessMove(CL, testLocation);
            //check that piece is of the opponent
            if ((testLocation.X >= 0 && testLocation.X <= 7) && (testLocation.Y >= 0 && testLocation.Y <= 7))
            {
                ChessPiece toPiece = board[testLocation];
                if (!SameColor(toPiece, color) && toPiece != ChessPiece.Empty)
                {
                    if (color == ChessColor.Black && toPiece == ChessPiece.WhiteKnight)
                        return true;
                    else if (color == ChessColor.White && toPiece == ChessPiece.BlackKnight)
                        return true;
                    else
                        return false;
                }
                //check all possible knight variations
            }
            return false;
        }

        static public bool putsKingInCheck(ChessBoard board, ChessMove move, ChessColor color) {
            ChessBoard resultingBoard = new ChessBoard(board.ToPartialFenBoard());
            resultingBoard.MakeMove(move);
            return kingInCheck(resultingBoard, findKing(resultingBoard, color), color);                        
        }

        static public bool kingInCheck(ChessBoard board, ChessLocation kingLocation, ChessColor color)
        {


            //check if night is checking
            //(x-2,y+1)(x-2,y-1)(x-1,y+2)(x-1,y-2)
            //(x+2,y+1)(x+2,y-1)(x+1,y+2)(x+1,y-2)
            //check all possible knight variations
            ChessLocation testPosition = new ChessLocation(kingLocation.X - 2, kingLocation.Y + 1);
            if (knightCheckingKing(board, kingLocation, testPosition, color)) 
                return true;
            testPosition.X = kingLocation.X - 2;
            testPosition.Y = kingLocation.Y - 1;
            if (knightCheckingKing(board, kingLocation, testPosition, color)) 
                return true;
            testPosition.X = kingLocation.X - 1;
            testPosition.Y = kingLocation.Y + 2;
            if (knightCheckingKing(board, kingLocation, testPosition, color)) 
                return true;
            testPosition.X = kingLocation.X - 1;
            testPosition.Y = kingLocation.Y - 2;
            if (knightCheckingKing(board, kingLocation, testPosition, color)) 
                return true;
            testPosition.X = kingLocation.X + 2;
            testPosition.Y = kingLocation.Y + 1;
            if (knightCheckingKing(board, kingLocation, testPosition, color)) 
                return true;
            testPosition.X = kingLocation.X + 2;
            testPosition.Y = kingLocation.Y - 1;
            if (knightCheckingKing(board, kingLocation, testPosition, color)) 
                return true;
            testPosition.X = kingLocation.X + 1;
            testPosition.Y = kingLocation.Y + 2;
            if (knightCheckingKing(board, kingLocation, testPosition, color)) 
                return true;
            testPosition.X = kingLocation.X + 1;
            testPosition.Y = kingLocation.Y - 2;
            if (knightCheckingKing(board, kingLocation, testPosition, color)) 
                return true;
            

            //check for all directions for attacking piece (queens and rooks on columns and rows, queens and bishops on diagnols, pawns on appropriate diagnol)
            
            //test Y direction Up (looking for queen or rook)
            ChessLocation CL = new ChessLocation(kingLocation.X, kingLocation.Y);
            ChessLocation testLocation = new ChessLocation(kingLocation.X - 2, kingLocation.Y + 1);
            ChessMove testX = new ChessMove(CL, testPosition);

            //check y position up
            testPosition.X = kingLocation.X;
            for (int y = kingLocation.Y+1; y < ChessBoard.NumberOfRows; y++)
            {
                testPosition.Y = y;
                //check that piece is of the opponent
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.Empty)
                        continue;
                    else if (!SameColor(toPiece, color))
                    {
                        if (color == ChessColor.Black && (toPiece == ChessPiece.WhiteRook || toPiece == ChessPiece.WhiteQueen))
                            return true;
                        else if (color == ChessColor.White && (toPiece == ChessPiece.BlackRook || toPiece == ChessPiece.BlackQueen))
                            return true;
                        break;
                    }
                    else
                        break;
                    //check all possible knight variations
                }
            }
            //test Y direction Down (looking for queen or rook)
            for (int y = kingLocation.Y-1; y >= 0; y--)
            {
                testPosition.Y = y;
                //check that piece is of the opponent
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.Empty)
                        continue;
                    else if (!SameColor(toPiece, color))
                    {
                        if (color == ChessColor.Black && (toPiece == ChessPiece.WhiteRook || toPiece == ChessPiece.WhiteQueen))
                            return true;
                        else if (color == ChessColor.White && (toPiece == ChessPiece.BlackRook || toPiece == ChessPiece.BlackQueen))
                            return true;
                        break;
                    }
                    else
                        break;
                    //check all possible knight variations
                }
            }
            testPosition.Y = kingLocation.Y;
            //test X direction Right (looking for queen or rook)
            for (int x = kingLocation.X+1; x < ChessBoard.NumberOfColumns; x++)
            {
                testPosition.X = x;
                //check that piece is of the opponent
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.Empty)
                        continue;
                    else if (!SameColor(toPiece, color))
                    {
                        if (color == ChessColor.Black && (toPiece == ChessPiece.WhiteRook || toPiece == ChessPiece.WhiteQueen))
                            return true;
                        else if (color == ChessColor.White && (toPiece == ChessPiece.BlackRook || toPiece == ChessPiece.BlackQueen))
                            return true;
                        break;
                    }
                    else
                        break;
                    //check all possible knight variations
                }
            }
            //test X direction Left (looking for queen or rook)
            for (int x = kingLocation.X-1; x >=0; x--)
            {
                testPosition.X = x;
                //check that piece is of the opponent
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.Empty)
                        continue;
                    else if (!SameColor(toPiece, color))
                    {
                        if (color == ChessColor.Black && (toPiece == ChessPiece.WhiteRook || toPiece == ChessPiece.WhiteQueen))
                            return true;
                        else if (color == ChessColor.White && (toPiece == ChessPiece.BlackRook || toPiece == ChessPiece.BlackQueen))
                            return true;
                        break;
                    }
                    else
                        break;
                    
                }
            }
            //******************test all bishop cases//**************************
            //test down right diagonol(looking for queen or bishop)
            testPosition.X = kingLocation.X;
            for (int y = kingLocation.Y + 1; y < ChessBoard.NumberOfRows; y++)
            {
                testPosition.Y = y;
                testPosition.X += 1;
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.Empty)
                        continue;
                    else if (!SameColor(toPiece, color))
                    {
                        if (color == ChessColor.Black && (toPiece == ChessPiece.WhiteBishop || toPiece == ChessPiece.WhiteQueen))
                            return true;
                        else if (color == ChessColor.White && (toPiece == ChessPiece.BlackBishop || toPiece == ChessPiece.BlackQueen))
                            return true;
                        break;
                    }
                    else
                        break;

                }
            }
            testPosition.X = kingLocation.X;
            //test up left diagonol(looking for queen or bishop)
            for (int y = kingLocation.Y - 1; y >= 0; y--)
            {
                testPosition.Y = y;
                testPosition.X -= 1;
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.Empty)
                        continue;
                    else if (!SameColor(toPiece, color))
                    {
                        if (color == ChessColor.Black && (toPiece == ChessPiece.WhiteBishop || toPiece == ChessPiece.WhiteQueen))
                            return true;
                        else if (color == ChessColor.White && (toPiece == ChessPiece.BlackBishop || toPiece == ChessPiece.BlackQueen))
                            return true;
                        break;
                    }
                    else
                        break;

                }
                
            }

            //test down left diagnol(looking for queen or bishop)
            testPosition.X = kingLocation.X;
            for (int y = kingLocation.Y + 1; y <= ChessBoard.NumberOfRows; y++)
            {
                testPosition.Y = y;
                testPosition.X -= 1;
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.Empty)
                        continue;
                    else if (!SameColor(toPiece, color))
                    {
                        if (color == ChessColor.Black && (toPiece == ChessPiece.WhiteBishop || toPiece == ChessPiece.WhiteQueen))
                            return true;
                        else if (color == ChessColor.White && (toPiece == ChessPiece.BlackBishop || toPiece == ChessPiece.BlackQueen))
                            return true;
                        break;
                    }
                    else
                        break;

                }

            }
            
            //test up right diagnol(looking for queen or bishop)
            testPosition.X = kingLocation.X;
            for (int y = kingLocation.Y - 1; y >= 0; y--)
            {
                testPosition.Y = y;
                testPosition.X += 1;
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.Empty)
                        continue;
                    else if (!SameColor(toPiece, color))
                    {
                        if (color == ChessColor.Black && (toPiece == ChessPiece.WhiteBishop || toPiece == ChessPiece.WhiteQueen))
                            return true;
                        else if (color == ChessColor.White && (toPiece == ChessPiece.BlackBishop || toPiece == ChessPiece.BlackQueen))
                            return true;
                        break;
                    }
                    else
                        break;

                }
                
            }
            //END BISHOP/QUEEN TEST**************************************//
            //***********CHECK PAWN CHECK*************//
            if (color == ChessColor.White)
            {
                testPosition.X = kingLocation.X + 1;
                testPosition.Y = kingLocation.Y - 1;
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.BlackPawn)
                        return true;
                }
                testPosition.X = kingLocation.X - 1;
                testPosition.Y = kingLocation.Y - 1;
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.BlackPawn)
                        return true;
                }
            }
            else if (color == ChessColor.Black)
            {
                testPosition.X = kingLocation.X - 1;
                testPosition.Y = kingLocation.Y + 1;
                
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.WhitePawn)
                        return true;
                }
                testPosition.X = kingLocation.X + 1;
                testPosition.Y = kingLocation.Y + 1;
                if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
                {
                    ChessPiece toPiece = board[testPosition];
                    if (toPiece == ChessPiece.WhitePawn)
                        return true;
                }
            }
            //*****************END PAWN CHECK**********************
            //*****************CHECK FOR KING CHECK****************
            
            testPosition.X = kingLocation.X - 1;
            testPosition.Y = kingLocation.Y + 1;
            if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
            {
                ChessPiece toPiece = board[testPosition];
                if (color == ChessColor.White && toPiece == ChessPiece.BlackKing)
                    return true;
                else if (color == ChessColor.Black && toPiece == ChessPiece.WhiteKing)
                    return true;
            }
            testPosition.X = kingLocation.X;
            testPosition.Y = kingLocation.Y + 1;
            if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
            {
                ChessPiece toPiece = board[testPosition];
                if (color == ChessColor.White && toPiece == ChessPiece.BlackKing)
                    return true;
                else if (color == ChessColor.Black && toPiece == ChessPiece.WhiteKing)
                    return true;
            }
            testPosition.X = kingLocation.X + 1;
            testPosition.Y = kingLocation.Y + 1;
            if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
            {
                ChessPiece toPiece = board[testPosition];
                if (color == ChessColor.White && toPiece == ChessPiece.BlackKing)
                    return true;
                else if (color == ChessColor.Black && toPiece == ChessPiece.WhiteKing)
                    return true;
            }
            testPosition.X = kingLocation.X - 1;
            testPosition.Y = kingLocation.Y;
            if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
            {
                ChessPiece toPiece = board[testPosition];
                if (color == ChessColor.White && toPiece == ChessPiece.BlackKing)
                    return true;
                else if (color == ChessColor.Black && toPiece == ChessPiece.WhiteKing)
                    return true;
            }
            testPosition.X = kingLocation.X + 1;
            testPosition.Y = kingLocation.Y;
            if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
            {
                ChessPiece toPiece = board[testPosition];
                if (color == ChessColor.White && toPiece == ChessPiece.BlackKing)
                    return true;
                else if (color == ChessColor.Black && toPiece == ChessPiece.WhiteKing)
                    return true;
            }
            testPosition.X = kingLocation.X - 1;
            testPosition.Y = kingLocation.Y - 1;
            if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
            {
                ChessPiece toPiece = board[testPosition];
                if (color == ChessColor.White && toPiece == ChessPiece.BlackKing)
                    return true;
                else if (color == ChessColor.Black && toPiece == ChessPiece.WhiteKing)
                    return true;
            }
            testPosition.X = kingLocation.X;
            testPosition.Y = kingLocation.Y - 1;
            if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
            {
                ChessPiece toPiece = board[testPosition];
                if (color == ChessColor.White && toPiece == ChessPiece.BlackKing)
                    return true;
                else if (color == ChessColor.Black && toPiece == ChessPiece.WhiteKing)
                    return true;
            }
            testPosition.X = kingLocation.X + 1;
            testPosition.Y = kingLocation.Y - 1;
            if ((testPosition.X >= 0 && testPosition.X <= 7) && (testPosition.Y >= 0 && testPosition.Y <= 7))
            {
                ChessPiece toPiece = board[testPosition];
                if (color == ChessColor.White && toPiece == ChessPiece.BlackKing)
                    return true;
                else if (color == ChessColor.Black && toPiece == ChessPiece.WhiteKing)
                    return true;
            }
            //*****************END KING CHECK**********************
            return false;
        }//End kingInCheck

        static public ChessLocation findKing(ChessBoard board, ChessColor color) {
            for (int y = 0; y < ChessBoard.NumberOfRows; ++y) {
                for (int x = 0; x < ChessBoard.NumberOfColumns; ++x) { 
                    if( color == ChessColor.Black ) {
                        if (board[x, y] == ChessPiece.BlackKing) {
                            return new ChessLocation(x, y);
                        }                        
                    }
                    else if (color == ChessColor.White) {
                        if (board[x, y] == ChessPiece.WhiteKing) {
                            return new ChessLocation(x, y);
                        }
                    }
                }
            }
            return null;
        }//End findKing

        static bool MoveIsOffBoard(ChessMove move) { 
            return (move.To.Y < 0 || move.To.Y > ChessBoard.NumberOfRows || move.To.X < 0 || move.To.X > ChessBoard.NumberOfColumns);
        }

        public static bool SameColor(ChessPiece piece, ChessColor color)
        {
            if (piece != ChessPiece.Empty)
            {
                if (piece < ChessPiece.Empty)
                {
                    if (color == ChessColor.Black) return true;
                }
                if (piece > ChessPiece.Empty)
                {
                    if (color == ChessColor.White) return true;
                }
            }
            return false;
        }
    }
}
